//
//  ZFBHomeViewController.m
//  01-ZFB
//
//  Created by My Mac on 2017/6/1.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ZFBHomeViewController.h"
#import "ZFBHomeTopView.h"
#import "ZFBHomeBottomView.h"
#import "ZFBHomeBottomList.h"
#import "ZFBHomeQRCodeViewController.h"

@interface ZFBHomeViewController ()<ZFBHomeTopViewDelegate,ZFBHomeBottomViewDelegate>

@end

@implementation ZFBHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ZFBHomeTopView *topView = [[ZFBHomeTopView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 115)];
    topView.backgroundColor = NAV_COLOR;
    topView.delegate = self;
    [self.view addSubview:topView];
    //加约束
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(115);
    }];
    
    ZFBHomeBottomView *bottomView = [[ZFBHomeBottomView alloc]init];
    bottomView.bottomDataArr = [self loadBottomListData];
    [self.view addSubview:bottomView];
    
    bottomView.delegate = self;
    //加约束
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(topView.mas_bottom).offset(0);
    }];
}

#pragma mark - 顶部按钮的代理方法
-(void)homeTopView:(ZFBHomeTopView *)topView topButtonClickType:(ZFBHomeTopViewButtonType)type title:(NSString *)title
{
    
    ZFBHomeQRCodeViewController *homeQRCodeVC = [ZFBHomeQRCodeViewController new];
    switch (type) {
        case ZFBHomeTopViewButtonTypeScan:
            homeQRCodeVC.navigationItem.title = title;
            homeQRCodeVC.view.backgroundColor = [UIColor lightGrayColor];
            [self.navigationController pushViewController:homeQRCodeVC animated:YES];
            break;
        case ZFBHomeTopViewButtonTypePay:
            [self pushChildViewTitle:title];
            break;
        case ZFBHomeTopViewButtonTypeCard:
            [self pushChildViewTitle:title];
            break;
        case ZFBHomeTopViewButtonTypeXiu:
            [self pushChildViewTitle:title];
            break;
        default:
            break;
    }
}

-(void)homeBottomView:(ZFBHomeBottomView *)bottomView title:(NSString *)title index:(NSInteger)index
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%ld",index + 1] message:title preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    //[self pushChildViewTitle:title];
}

-(void)pushChildViewTitle:(NSString *)title
{
    UIViewController *viewController = [[UIViewController alloc]init];
    viewController.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
    viewController.navigationItem.title = title;
    [self.navigationController pushViewController:viewController animated:YES];
}

-(NSArray *)loadBottomListData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"homeAllBottomLists.plist" ofType:nil];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *marr = [NSMutableArray new];
    
    for (NSDictionary *dicr in arr) {
        ZFBHomeBottomList *bottomList = [ZFBHomeBottomList bottonListWithDict:dicr];
        [marr addObject:bottomList];
    }
    return marr.copy;
}

@end
